<?php
namespace app\index\controller;
use think\Db;
use app\common\redis\RedisPackage;
class Index extends Common
{

    /**
     * 管理权限
     */
    public function quanxian(){
        $id = $this->id;
        $result_lb_id = db('jujian')->where('id',$id)->value('lb_id');
        $result_quanxian = db('jujian_lb')->where('id',$result_lb_id)->value('qx');
        $result = explode(',',$result_quanxian);
        returnSuccess($result);
    }

    /**
     * 获取首页搜索居间的下拉框数据（如果为三级居间则包含自己）
     */
    public function getJujianSons(){
        $id = $this->id;
        $name = $this->bh;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        $sql = "SELECT id,CONCAT(bh,'-',xingming) as bh FROM jujian where `sj_xulie` like '%,$id,%' AND LOWER(LEFT(bh,3)) != 'czy' order by length(`bh`) asc,`bh` asc ";
        $jujian_bhs = db()->query($sql);
        if(strlen($name) >= 4 && !$this->isCZY){
            $jujian= ['id'=>$id,'bh'=>$name];
            array_unshift($jujian_bhs,$jujian);
        }
        returnSuccess($jujian_bhs);
    }
    /**
     * 获取路由组
     */
    public function getluyou(){
        $ib_id = $this->jujian['gt_jujian_id'];
        if($this->isCZY){
            $ib_id = db('jujian')->where('id',$this->jujian['par_id'])->value('gt_jujian_id');
        }
        $where['bz'] = 1;
        $where['ib_id'] =$ib_id;
        $luyou = db('luyou_zu')->where($where)->field('id,name')->select();
        returnSuccess($luyou);
    }


    /**
     * 首页数据表格
     */
    public function indexTable(){
        $id = $this->id;
        // 如果是操作员，查找上级，继承其id
        if ($this->isCZY) {
            $id = $this->jujian['par_id'];
        }
//        $page = input('page/d') ? input('page/d') : 1;
//        $limit = input('limit/d') ? input('limit/d') : 20;
        $where['yonghu.bz'] = 1;
        $where['yonghu_bian.hb_id'] = ['in',cache('hb_array')]; //货币的类型（1人民币2港币3美元4欧元）
        if(input('name')){
            $name=trim(input('name'));
            $where['yonghu.bh|yonghu.xingming|yonghu.tel'] = ['like', "%$name%"];
        }
//        if(input('hb_id/d') && in_array(input('hb_id/d'),cache('hb_array'))){
//            $where['yonghu_bian.hb_id'] = input('hb_id/d');
//        }
//        if(input('tel')){
//            $where['yonghu.tel'] = input('tel');
//        }
//        if(input('luyou_id/d')){
//            $where['yonghu.luyou_zu_id'] = input('luyou_id/d');
//        }
        //选择居间查询
        $sonJujianId = trim(input('son_jujian_id'),',');
        $or = '';
        if($sonJujianId){
            $sonJujianId = explode(',',$sonJujianId);
            if(count($sonJujianId) >=1){
                if(count($sonJujianId) ==1) {
                    //判断选择的是不是自己
                    if ($sonJujianId[0] == $this->id) {
                        $where['yonghu.jujian_id'] = $sonJujianId[0];
                    }else{
                        $where['yonghu.sj_xulie'] = ['like', "%,$sonJujianId[0],%"];
                    }
                }else {
                    foreach ($sonJujianId as $k => $v) {
                        if(is_numeric($v) && $v>0){
                            if($or == ''){
                                $or .= " (yonghu.sj_xulie like '%,$v,%'";
                            }else{
                                $or .= " or yonghu.sj_xulie like '%,$v,%'";
                            }
                        }
                    }
                    if($or != ''){
                        $or .= ')';
                    }
                }
            }
        }else{
            $where['yonghu.sj_xulie'] = ['like',"%,$id,%"];
        }
//        //排序
//        $order = [];
//        if(input('order_field') && input('order')){
//            $order[input('order_field')] =  input('order');
//        }
//        //总条目
//        $count = db('yonghu')
//            ->field('count(*) as count,SUM(jine) as jine,SUM(dongjie_jine) as dongjie_jine,SUM(float_yk) as float_yk')
//            ->join('yonghu_bian','yonghu.id = yonghu_bian.yh_id','LEFT')
//            ->join('luyou_zu','yonghu.luyou_zu_id = luyou_zu.id','LEFT')
//            ->join('baozheng_zu','yonghu.baozheng_zu_id = baozheng_zu.id','LEFT')
//            ->where($where)
//            ->where($or)
//            ->find();
//        $renshu = db('yonghu')
//            ->field('yonghu.id')
//            ->group('yonghu.id')
//            ->join('yonghu_bian','yonghu.id = yonghu_bian.yh_id','LEFT')
//            ->join('luyou_zu','yonghu.luyou_zu_id = luyou_zu.id','LEFT')
//            ->join('baozheng_zu','yonghu.baozheng_zu_id = baozheng_zu.id','LEFT')
//            ->where($where)
//            ->where($or)
//            ->select();
//        $count['renshu'] = count($renshu);
        //数据
        $data = db('yonghu')
            ->field('yonghu.sfz_bh,yonghu.id,yonghu.sj_xulie,yonghu.bh,yonghu.xingming,yonghu.tel,huobi.name as huobi,yonghu_bian.jine,yonghu_bian.dongjie_jine,yonghu_bian.float_yk,yonghu_bian.ping_jine,yonghu_bian.geye_jine,baozheng_zu.name as bz_name,luyou_zu.name as ly_name,yonghu.zhuce_shijian')
            ->where($where)
            ->join('yonghu_bian','yonghu.id = yonghu_bian.yh_id','LEFT')
            ->join('luyou_zu','yonghu.luyou_zu_id = luyou_zu.id','LEFT')
            ->join('baozheng_zu','yonghu.baozheng_zu_id = baozheng_zu.id','LEFT')
            ->join('huobi','yonghu_bian.hb_id = huobi.id')
//            ->page($page,$limit)
//            ->where($or)
//            ->order($order)
            ->select();
        //生成代理关系
        foreach ($data as $k=>$v){
            $data[$k]['agents'] = $this->get_dl_name($v['sj_xulie']);
            unset($data[$k]['sj_xulie']);
            if(config('web_name') == 'HC'){
                if(!$this->is_Dingji){
                    if(preg_match("/^1[345678]{1}\d{9}$/",$data[$k]['bh'])){
                        $data[$k]['bh'] = substr_replace($data[$k]['bh'],'****',3,4);
                    }
                    if(preg_match("/^1[345678]{1}\d{9}$/",$data[$k]['tel'])){
                        $data[$k]['tel'] = substr_replace($data[$k]['tel'],'****',3,4);
                    }
                }
            }
        }
        if($data){
            returnSuccess(['count'=>[],'data'=>$data]);
        }else{
            returnError('没有数据！');
        }

    }

/** 添加用户
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
public function addUser(){
    $gt_jujian_id = db('jujian')->where('id',$this->id)->value('gt_jujian_id');
    $result['sheng'] = db('sheng')->select();
    $result['bank'] = db('bank')->select();
    $result['shouxufei'] = db('fee_yonghu_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->select();
    $result['baozhengjin'] = db('baozheng_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->select();
    $result['fengkong'] = db('fengkong_moban')->where('ib_id',$gt_jujian_id)->where('bz',1)->select();
    $result['huilv'] = db('huobi_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->select();
    $result['luyou']  = db('luyou_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->select();
    $result['chujin'] = db('chujin_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->select();
    $result['rujin'] = db('rujin_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->select();
    returnSuccess($result);
}

/**
 * 获取市
 * @param Request $request
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
public function getShi()
{
    $sheng_id = input('sheng_id/d');
    $shi =  db('shi')->where('sheng_id',$sheng_id)->select();
    if(!$shi){
        returnError('未获取到数据！');
    }else{
        returnSuccess($shi);
    }
}



/**
 * 添加用户提交
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
public function addUserRequest()
{
    //-----------------------------------------验证参数-----------------------------------
    //密码
    $mm = input('mm');
    if(!$mm) {returnError('密码不可为空');}
    $mm = md5($mm);
    //姓名
    $xm = input('xm');
    if(!$xm) {returnError('姓名不可为空');}
    //电话
    $dh = input('dh');
    if(!$dh){returnError('电话不可为空');}
    if(db('yonghu')->where('tel',$dh)->find()) {returnError('电话被占用');}
    //身份证号
    $sfzh = input('sfzh');
    if(!$sfzh){returnError('身份证不可为空');}
    if(db('yonghu')->where('sfz_bh',$sfzh)->find()) {returnError('身份证被占用');}

    $sheng = input('sheng/d');
    if($sheng == 0) {returnError('省不可为空');}

    $shi = input('shi/d');
    if($shi<=0){ returnError('市不可为空');}

    $khh = input('khh/d');
    if($khh<=0) {returnError('开户行不可为空');}

    $khhdz = input('khhdz');
    if(!$khhdz ){returnError('开户地址不可为空');}

    $yhkh = input('yhkh');
    if(!$yhkh ) {returnError('银行卡号不可为空');}

    $sxfmb = input('sxfmb/d');
    if(!$sxfmb ){ $sxfmb = db('fee_jujian_zu')->where('moren_bz',1)->value('id');}

    $bzjmb = input('bzjmb/d');
    if(!$bzjmb ){ $bzjmb = db('baozheng_zu')->where('moren_bz',1)->value('id');}

    $fkmb = input('fkmb/d');
    if(!$fkmb ){ $fkmb = db('fengkong_moban')->where('moren_bz',1)->value('id');}

    $hlmb = input('hlmb/d') ;
    if(!$hlmb ){ $hlmb = db('huobi_zu')->where('moren_bz',1)->value('id');}

    $lyz = input('lyz/d');
    if(!$lyz ){ $lyz = db('luyou_zu')->where('moren_bz',1)->value('id');}

    $chujinzu = input('chujinzu/d');
    if(!$chujinzu ){  $chujinzu = db('chujin_zu')->where('moren_bz',1)->value('id');}

    $rujinzu = input('rujinzu/d');
    if(!$rujinzu ){ $rujinzu = db('rujin_zu')->where('moren_bz',1)->value('id');}
    //上级编号
    $sjbh = input('sjbh');
    if(!$sjbh ) {returnError('请输入上级编号');}
    if(config('web_name') == 'HC'){
        if(strlen($sjbh) != 7) {returnError('上级格式错误');}
    }
    if(substr($sjbh,0,3) == 'CZY'){
        returnError('上级不可为操作员');
    }
    //判断是否有权限
    $par = db('jujian')->where('bh',$sjbh)->where('bz',1)->find();
    if(!$par){returnError('上级不存在');}
    $sj_id_arr =  explode(',',$par['sj_xulie']);
    $id = $this->id;
    $bh = $this->jujian['bh'];
    if($this->isCZY){
        $id = $this->jujian['par_id'];
        $bh = db('jujian')->where('id',$id)->value('bh');
    }
    if(!in_array($id,$sj_id_arr) && $bh != $sjbh){
        returnError('当前登录账户无权限添加到编号'.$sjbh.'下');
    }
    //-----------------------------------------验证参数结束-----------------------------------
    $num = 0;
    for ($i = 1;$i<=strlen($sjbh);$i++){
        $n = substr($sjbh,strlen($sjbh)-$i,1);
        if(is_numeric($n)){
            $num ++;
        }
    }
    if($num == 0){
        for($i= 0 ;$i < 5;$i++){
            $sjbh .= '0';
        }
    }elseif ($num == 2){
        for($i= 0 ;$i < 3;$i++){
            $sjbh .= '0';
        }
    }elseif($num == 5){

    }else{
        returnError('上级编号错误，无法生成编号');
    }
    $user_bh = db('yonghu')->where('bh','like',$sjbh.'%')->order('bh','desc')->value('bh'); //拿到最后最大的一个编号
    if(!$user_bh){
        $user_bh = $sjbh . '001';
    }else{
        $user_bh = substr($user_bh,-3);//拿到后三位
        if($user_bh == 999){
            returnError('当前上级编号下用户已满');
        }
        for($i = 1 ;$i <= 999;$i++){ //不要4
            $user_bh++;
            if($user_bh > 999){
                returnError('当前上级编号下用户已满');
            }
            $user_bh += 1000;
            $user_bh = substr($user_bh,1);
            if(strpos($user_bh,'4') === false){ //不要4
                $user_bh = $sjbh . $user_bh;
                break;
            }
        }
    }
    //生成序列
    $xl = ','.$par['id'].$par['sj_xulie'];
    $zhuce_shijian = time();
    $shuju = array('bh'=>$user_bh
    ,'passwd'=>$mm
    ,'xingming'=>$xm
    ,'tel'=>$dh
    ,'sfz_bh'=>$sfzh
    ,'sheng_id'=>$sheng
    ,'shi_id'=>$shi
    ,'bank_id'=>$khh
    ,'dizhi'=>$khhdz
    ,'bank_bh'=>$yhkh
    ,'fee_zu_id'=>$sxfmb
    ,'baozheng_zu_id'=>$bzjmb
    ,'fengkong_zu_id'=>$fkmb
    ,'huobi_zu_id'=>$hlmb
    ,'luyou_zu_id'=>$lyz
    ,'rujin_zu_id'=>$rujinzu
    ,'chujin_zu_id'=>$chujinzu
    ,'jujian_id'=>$par['id'] //直属上级（jujian）
    ,'gt_jujian_id'=>$par['gt_jujian_id'] //直属上级（jujian）
    ,'sj_xulie'=>$xl
    ,'zhuce_shijian'=>$zhuce_shijian);

    //开启事务
    Db::startTrans();
    //插入用户表
    $uid = db('yonghu')->insertGetId($shuju);
    //货币
    $yonghu_bian_shuju = array(['yh_id'=>$uid,'hb_id'=>1],['yh_id'=>$uid,'hb_id'=>2],['yh_id'=>$uid,'hb_id'=>3],['yh_id'=>$uid,'hb_id'=>4]);
    $result = db('yonghu_bian')->insertAll($yonghu_bian_shuju);
    if($uid && is_int($result)){
        Db::commit();
        returnSuccess('','添加成功！');
    }else{
        Db::rollback();
        returnError('添加失败，请重试~！');
    }
    //记录日志功能
    $this->riZhi(28,['添加用户'=>'成功','操作人员'=>$this->jujian['bh'],'用户ID'=>$uid,'用户编号'=>$user_bh,'用户姓名'=>$xm,'身份证号'=>$sfzh],['update_bz'=>'1']);
}

/**用户编辑请求
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
public function editUser(){
    $xz_id = input('xz_id/d');
    $yonghu = db('yonghu')->field('id,xingming,tel,sfz_bh,sheng_id,shi_id,bank_id,dizhi,bank_bh,fee_zu_id,baozheng_zu_id,fengkong_zu_id,huobi_zu_id,luyou_zu_id,chujin_zu_id,rujin_zu_id,jujian_id')->where('id',$xz_id)->find();
    if(!$yonghu){
        returnError('用户不存在');
    }
    $pbh = db('jujian')->where('id',$yonghu['jujian_id'])->value('bh');//拿到上级编号
    $yonghu['par_bh'] = $pbh;
    //获取顶级的居间ID
    $gt_jujian_id = db('jujian')->where('id',$this->id)->value('gt_jujian_id');
    $data['sheng'] = db('sheng')->field('id,sheng')->select();
    $data['shi'] = db('shi')->where('sheng_id',$yonghu['sheng_id'])->field('id,shi')->select();
    $data['bank'] = db('bank')->field('id,name')->select();
    $data['fee_yonghu_zu'] = db('fee_yonghu_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->field('id,name')->select();
    $data['baozheng_zu'] = db('baozheng_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->field('id,name')->select();
    $data['fengkong_moban'] = db('fengkong_moban')->where('ib_id',$gt_jujian_id)->where('bz',1)->field('id,name')->select();
    $data['huobi_zu'] = db('huobi_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->field('id,name')->select();
    $data['luyou_zu'] = db('luyou_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->field('id,name')->select();
    $data['chujin_zu'] = db('chujin_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->field('id,name')->select();
    $data['rujin_zu'] = db('rujin_zu')->where('ib_id',$gt_jujian_id)->where('bz',1)->field('id,name')->select();
    $data['yonghu'] = $yonghu;
    returnSuccess($data);
}

/**编辑用户提交接口
 * @throws \think\Exception
 * @throws \think\exception\PDOException
 */
public function editUserRequest(){
    if(!$this->is_Dingji){
        returnError('当前登录居间不可编辑用户！');
    }
    //-----------------------------------------验证参数-----------------------------------
    $id =input('id/d');
    if(!$id) {returnError('用户不存在');}
    //姓名
    $xm = input('xm');
    if(!$xm) {returnError('姓名不可为空');}
    //电话
    $dh = input('dh');
    if(!$dh){returnError('电话不可为空');}
    //身份证号
    $sfzh = input('sfzh');
    if(!$sfzh){returnError('身份证不可为空');}
    $sheng = input('sheng/d');
    if($sheng == 0) {returnError('请选择省份');}

    $shi = input('shi/d');
    if($shi<=0){ returnError('请选择市');}

    $khh = input('khh/d');
    if($khh<=0) {returnError('请选择开户行');}

    $khhdz = input('khhdz');
    if(!$khhdz ){ returnError('请输入开户地址');}

    $yhkh = input('yhkh');
    if(!$yhkh ) {returnError('请输入银行卡号');}

    $sxfmb = input('sxfmb/d');
    if(!$sxfmb ){returnError('请选择手续费模板');}

    $bzjmb = input('bzjmb/d');
    if(!$bzjmb ){ returnError('请选择保证金模板');}

    $fkmb = input('fkmb/d');
    if(!$fkmb ){ returnError('请选择风控模板');}

    $hlmb = input('hlmb/d') ;
    if(!$hlmb ){ returnError('请选择汇率模板');}

    $lyz = input('lyz/d');
    if(!$lyz ){ returnError('请选择路由模板');}

    $chujinzu = input('chujinzu/d');
    if(!$chujinzu ){ returnError('请选择出金模板');}

    $rujinzu = input('rujinzu/d');
    if(!$rujinzu ){ returnError('请选择入金模板');}
    $user = db('yonghu')->where('id',$id)->where('bz',1)->find();
    if(!$user){
        returnError('编辑的用户不存在');
    }
    $par= db('jujian')->where('id',$user['jujian_id'])->where('bz',1)->find();
    if(!$par){
        returnError('编辑的用户上级不存在');
    }
    $user_shouxufei_muban = db('fee_yonghu_zu')->where('id',$sxfmb)->find();
    if(!$user_shouxufei_muban){
        returnError('手续费模板不存在');
    }
    //-----------------------------------------验证参数结束-----------------------------------
    if($par['lb_id'] != 1 && $user['fee_zu_id'] != $sxfmb){
        $user_heyues = db('fee_yonghu_zu_detail')->where('fee_zu_id',$sxfmb)->select();
        foreach ($user_heyues as $k=>$v){
            $max = db('jujian')
                ->alias('j')
                ->where('j.id', $user['jujian_id'])
                ->join('fee_jujian_zu_detail fjd','fjd.fee_zu_id = j.fee_zu_id and fjd.heyue_zu_id = '.$v['heyue_zu_id'],'LEFT')
                ->join('fee_jujian_zu fj','fj.ib_id = j.gt_jujian_id and fj.moren_bz = 1','LEFT')
//                ->join('fee_jujian_zu_detail fjd1','fjd1.fee_zu_id = fj.id and fjd1.heyue_zu_id = '.$v['heyue_zu_id'],'LEFT')
//                ->field("if(`fjd`.`jian_yongjin`,`fjd`.`jian_yongjin`,`fjd1`.`jian_yongjin`) as jian_yongjin,if(`fjd`.`ping_zuo_yongjin`,`fjd`.`ping_zuo_yongjin`,`fjd1`.`ping_zuo_yongjin`) as ping_zuo_yongjin,if(`fjd`.`ping_jin_yongjin`,`fjd`.`ping_jin_yongjin`,`fjd1`.`ping_jin_yongjin`) as ping_jin_yongjin")
                ->field("`fjd`.`jian_yongjin` as jian_yongjin,`fjd`.`ping_zuo_yongjin` as ping_zuo_yongjin,`fjd`.`ping_jin_yongjin` as ping_jin_yongjin")
                ->find();
            if($max['jian_yongjin'] != null && $max['ping_zuo_yongjin'] != null && $max['ping_jin_yongjin'] != null){
                if($v['jian_yongjin'] < $max['jian_yongjin'] || $v['ping_zuo_yongjin'] < $max['ping_zuo_yongjin'] || $v['ping_jin_yongjin'] < $max['ping_jin_yongjin']){
                    returnError("错误：当前手续费模板合约费率（建：{$v['jian_yongjin']}平昨：{$v['ping_zuo_yongjin']} 平今：{$v['ping_zuo_yongjin']}）小于上级居间费率（建：{$max['jian_yongjin']}平昨：{$max['ping_zuo_yongjin']} 平今：{$max['ping_zuo_yongjin']}）");
                }
            }

        }
    }
    $shuju = array(
        'xingming'=>$xm
    ,'tel'=>$dh
    ,'sfz_bh'=>$sfzh
    ,'sheng_id'=>$sheng
    ,'shi_id'=>$shi
    ,'bank_id'=>$khh
    ,'dizhi'=>$khhdz
    ,'bank_bh'=>$yhkh
    ,'fee_zu_id'=>$sxfmb
    ,'baozheng_zu_id'=>$bzjmb
    ,'fengkong_zu_id'=>$fkmb
    ,'huobi_zu_id'=>$hlmb
    ,'luyou_zu_id'=>$lyz
    ,'rujin_zu_id'=>$rujinzu
    ,'chujin_zu_id'=>$chujinzu
    );
    $bool = db('yonghu')->where('id',$id)->update($shuju);
    if($bool){
        $this->riZhi(26,['编辑用户'=>'成功','操作人员'=>$this->jujian['bh'],'姓名'=>$xm,'chujin_zu_id'=>$chujinzu,'rujin_zu_id'=>$rujinzu,'baozheng_zu_id'=>$bzjmb,'fengkong_zu_id'=>$fkmb,'huobi_zu_id'=>$hlmb,'luyou_zu_id'=>$lyz],['update_bz'=>'1']);
        returnSuccess('更新成功');
    }else{
        returnError('更新失败');
    }
}


/**
 * 调整资金接口
 */
public function editMoney(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->field('id,xingming,bh')->find();
    if(!$user){
        returnError('用户不存在');
    }
    $hb = input('hb');
    if($hb == '人民币'){
        $hb_id = 1;
    }elseif($hb == '港币'){
        $hb_id = 2;
    }elseif($hb == '美元'){
        $hb_id = 3;
    }elseif($hb == '欧元'){
        $hb_id = 4;
    }else{
        returnError('请选择货币类型');
    }
    $money = db('yonghu_bian')->where(['yh_id'=>$id,'hb_id'=>$hb_id])->value('jine');
    $user['jine'] = $money;
    $user['hb'] = $hb;
    returnSuccess($user);
}
/**
 * 调整资金提交接口
 * @return mixed
 */
public function editMoneyRequest(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->find();
    if(!$user){
        returnError('用户不存在');
    }
    $hb = input('hb');
    if($hb == '人民币'){
        $hb_id = 1;
    }elseif($hb == '港币'){
        $hb_id = 2;
    }elseif($hb == '美元'){
        $hb_id = 3;
    }elseif($hb == '欧元'){
        $hb_id = 4;
    }else{
        returnError('请选择货币类型');
    }
    $lb = input('lb_id/d'); //调整资金的类别
    $money = input('money');
    if (!is_numeric($money)) returnError("金额有误");
    if(!$lb){
        returnError('请选择类别');
    }
//    if($lb == 15){
//        if($money >= 0){
//            returnError('当前选择类别金额只能为负数');
//        }
//    }else{
//        if($money <= 0){
//            returnError('当前选择类别金额只能为正数');
//        }
//    }
    Db::startTrans();
    $res1 = db('yonghu_bian')->where(['hb_id'=>$hb_id,'yh_id'=>$id])->setInc('jine',$money);
    $money1 = db('yonghu_bian')->where(['hb_id'=>$hb_id,'yh_id'=>$id])->value('jine');
    $res2 = db('yonghu_liushui')->insert(['yh_id'=>$id,'lb'=>$lb,'jine'=>$money,'yue'=>$money1,'hb_id'=>$hb_id,'shijian'=>time()]);
    if($res1 && $res2){
        Db::commit();
        returnSuccess('调整成功');
    }else{
        Db::rollback();
        returnError('调整失败');
    }
    $this->riZhi(121,['资金调整'=>'成功','操作人员'=>$this->jujian['bh'],'用户ID'=>$id,'货币ID'=>$hb_id,'调整金额'=>$money,'调整后余额'=>$money1],['update_bz'=>'1']);
    $arr = '4;'.$id.';2;'.$hb_id.';2;';
    RedisPackage::RPush('task_list',$arr);
}

/**
 * 删除用户
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
public function delUser(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->find();
    if(!$user){
        returnError('用户不存在');
    }

    //判断持仓
    $cicang = db('order_jl_num')
        ->alias('o')
        ->field('o.*,y.id as uid,y.bh,hy.name as heyue,hb.name as bizhong')
        ->join('yonghu y', 'o.yh_id=y.id')
        ->join('heyue hy', 'o.heyue_id=hy.id')
        ->join('huobi hb', 'o.hb_id=hb.id')
        ->where('o.bz', 1)
        ->where('y.id', $id)
        ->find();
    if($cicang){
        returnError('该用户还有持仓订单，无法删除！');
    }
    //判断用户的余额
    $del = db('yonghu')->where('id',$id)->update(['bz'=>3]);
    if($del){
        returnSuccess('删除成功');
    }else{
        returnError('删除失败');
    }
    $this->riZhi(58,['删除用户'=>'成功','操作人员'=>$this->jujian['bh'],'用户ID'=>$id,'用户编号'=>$user['bh'],'用户姓名'=>$user['xingming']],['update_bz'=>'1']);
}

/**
 * 用户转挂请求接口
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
public function userSwitch(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->find();
    if(!$user){
        returnError('用户不存在');
    }
    $data['bh'] = $user['bh'];
    $data['par_bh'] = db('jujian')->where('id',$user['jujian_id'])->value('bh');
    returnSuccess($data);
}

/**
 * 用户转挂提交接口
 * @throws \think\Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @throws \think\exception\PDOException
 */
public function userSwitchRequest(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->find();
    if(!$user){
        returnError('用户不存在');
    }
    $newPar_bh = input('newPar_bh');
    if(substr($newPar_bh, 0, 3) == 'CZY'){
        returnError('只能转挂到操作员');
    }
    if(config('web_name') == 'HC'){
        if(strlen($newPar_bh) != 7){
            returnError('只能转挂到三级居间');
        }
    }

    $jujian = db('jujian')->where('bh',$newPar_bh)->find();
    if(!$jujian){
        returnError('上级不存在');
    }
    $sj_xulie = ','.$jujian['id'].$jujian['sj_xulie'];
    $result = db('yonghu')->where('id',$id)->update(['sj_xulie'=>$sj_xulie,'jujian_id'=>$jujian['id']]);
    if($result){
        returnSuccess('转挂成功');
    }else{
        returnError('转挂失败');
    }
    $this->riZhi(113,['用户转挂'=>'成功','操作人员'=>$this->jujian['bh'],'转挂用户编号'=>$user['bh'],'转挂后上级编号'=>$newPar_bh],['update_bz'=>'1']);
}

/**
 * 用户重置密码
 * @throws \think\Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @throws \think\exception\PDOException
 */
public function resetPass(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->find();
    if(!$user){
        returnError('用户不存在');
    }
    $mm = md5('888888');
    $result_yonghu = db('yonghu')->where('id',$id)->update(['passwd'=>$mm]);
    if($result_yonghu){
        $this->riZhi(108,['修改密码'=>'成功','修改用户'=>$user['bh'],'操作人员'=>$this->id,'修改后密码'=>'******'],['update_bz'=>'1']);
        returnSuccess('密码已重置为888888');
    }else{
        returnError('密码重置失败，请尝试初始密码888888');
    }
}

/**
 * 委托单查询
 * @return array
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
public function weiTuoQuery(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->find();
    if(!$user){
        returnError('用户不存在');
    }
    $agents = $this->get_dl_name($user['sj_xulie']);
    $page = input('page/d') > 0 ? input('page/d') : 1;
    $limit = input('limit/d') > 0 ? input('limit/d') : 20;
    $startTime = input('startTime',false,'strtotime') ? input('startTime','','strtotime') : strtotime(date('Y-m-d',time()));
    $endTime = input('endTime',false,'strtotime') ? input('endTime','','strtotime')  : strtotime(date('Y-m-d',time()));
    $endTime += 24 * 3600;
    $data = db('order_jl') //lb1买kp_bz1开仓
    ->field('yonghu.bh,xingming,heyue.name,baodan,baodan,bianhao,lb,baodan_zt.miaoshu,kp_bz,num,cj_num,tuo_price,yongjin,hb_id,kaiping_lb.name as kp_name,tuo_shijian,cj_shijian,zhanghao_id')
        ->join('yonghu','yonghu.id=order_jl.yh_id','LEFT')
        ->page($page,$limit)
        ->where('yonghu.id',$id)
        ->where('tuo_shijian','between time',[(int)$startTime,(int)$endTime])
        ->order('order_jl.id desc,order_jl.tuo_shijian desc')
        ->join('baodan_zt','order_jl.baodan_bz=baodan_zt.id','LEFT')
        ->join('kaiping_lb','order_jl.kp_lb=kaiping_lb.id','LEFT')
        ->join('heyue','order_jl.heyue_id=heyue.id','LEFT')
        ->select();
    $count = db('order_jl')
        ->join('yonghu','yonghu.id=order_jl.yh_id','LEFT')
        ->where('yonghu.id',$id)
        ->where('tuo_shijian','between time',[(int)$startTime,(int)$endTime])
        ->order('order_jl.id desc,order_jl.tuo_shijian desc')
        ->join('baodan_zt','order_jl.baodan_bz=baodan_zt.id','LEFT')
        ->join('kaiping_lb','order_jl.kp_lb=kaiping_lb.id','LEFT')
        ->join('heyue','order_jl.heyue_id=heyue.id','LEFT')
        ->count();
    foreach ($data as $k =>$v){
        $data[$k]['agents'] = $agents;
        if($data[$k]['lb'] == 1){
            $data[$k]['lb'] = '买';
        }else{
            $data[$k]['lb'] = '卖';
        }
        if($data[$k]['hb_id'] == 1){
            $data[$k]['hb_id'] = '人民币';
        }elseif($data[$k]['hb_id'] == 2){
            $data[$k]['hb_id'] = '港币';
        }elseif($data[$k]['hb_id'] == 3){
            $data[$k]['hb_id'] = '美元';
        }elseif($data[$k]['hb_id'] == 4){
            $data[$k]['hb_id'] = '欧元';
        }
        if($data[$k]['zhanghao_id'] > 0){
            $data[$k]['zhanghao_id'] = '弘苏组';
        }else{
            $data[$k]['zhanghao_id'] = '盈透组';
        }
        if($data[$k]['tuo_price'] == 0){
            $data[$k]['tuo_price'] = '市价成交';
        }
        if($data[$k]['kp_bz'] == 1){
            $data[$k]['kp_bz'] = '开仓';
        }else{
            $data[$k]['kp_bz'] = '平仓';
        }
//        if($data[$k]['tuo_shijian'] > 0){
//            $data[$k]['tuo_shijian'] = date('Y-m-d H:i:s',$data[$k]['tuo_shijian']);
//        }else{
//            $data[$k]['tuo_shijian'] = '-';
//        }
//        if($data[$k]['cj_shijian'] > 0){
//            $data[$k]['cj_shijian'] = date('Y-m-d H:i:s',$data[$k]['cj_shijian']);
//        }else{
//            $data[$k]['cj_shijian'] = '-';
//        }
    }
    if(!empty($data)){
        returnSuccess(['count'=>$count,'data'=>$data]);
    }else{
        returnError('未查询到数据');
    }
}




public function pcdQuery(){
    $id = input('id/d');
    $user = db('yonghu')->where('id',$id)->find();
    if(!$user){
        returnError('用户不存在');
    }
    $agents = $this->get_dl_name($user['sj_xulie']);
    $page = input('page/d') > 0 ? input('page/d') : 1;
    $limit = input('limit/d') > 0 ? input('limit/d') : 20;
    $startTime = input('startTime',false,'strtotime') ? input('startTime','','strtotime') : strtotime(date('Y-m-d',time()));
    $endTime = input('endTime',false,'strtotime') ? input('endTime','','strtotime')  : strtotime(date('Y-m-d',time()));
    $endTime += 24 * 3600;
    $where['yonghu.id'] = $id;
    $where['order_jl.kp_bz'] = 2;//平仓
    $where['order_jl.zong_cj_num'] = ['>',0];
    $where['order_jl.tuo_shijian'] = ['between time',[(int)$startTime,(int)$endTime]];
    $data =  db('order_jl')
        ->field('heyue_zu.heyue_cs,yonghu.bh,yonghu.xingming,heyue.name as heyuename,order_jl.baodan,order_jl.bianhao,order_jl.lb,baodan_zt.miaoshu,order_jl.cj_num,jian_price,cj_price,yongjin,order_jl.hb_id,kaiping_lb.name,tuo_shijian,cj_shijian,zhanghao_id')
        ->join('yonghu','yonghu.id=order_jl.yh_id','LEFT')
        ->join('baodan_zt','baodan_zt.id=baodan_bz','LEFT')
        ->join('kaiping_lb','kaiping_lb.id=kp_lb','LEFT')
        ->join('heyue','heyue.id=heyue_id','LEFT')
        ->join('heyue_zu','heyue_zu.id=heyue_zu_id','LEFT')
        ->page($page,$limit)
        ->where($where)
        ->group('order_jl.zong_baodan')
        ->order('order_jl.id desc,order_jl.tuo_shijian desc')
        ->select();

    $count =  db('order_jl')
        ->join('yonghu','yonghu.id=order_jl.yh_id','LEFT')
        ->join('baodan_zt','baodan_zt.id=baodan_bz','LEFT')
        ->join('kaiping_lb','kaiping_lb.id=kp_lb','LEFT')
        ->join('heyue','heyue.id=heyue_id','LEFT')
        ->join('heyue_zu','heyue_zu.id=heyue_zu_id','LEFT')
        ->where($where)
        ->group('order_jl.zong_baodan')
        ->order('order_jl.id desc,order_jl.tuo_shijian desc')
        ->count();

    foreach ($data as $k =>$v){
        $data[$k]['agents'] = $agents;
        if($data[$k]['lb'] == 1){
            $data[$k]['lb'] = '买';
            $data[$k]['yingkui'] = ($data[$k]['jian_price'] - $data[$k]['cj_price']) * $data[$k]['heyue_cs'] * $data[$k]['cj_num'];
        }else{
            $data[$k]['lb'] = '卖';
            $data[$k]['yingkui'] = ($data[$k]['cj_price'] - $data[$k]['jian_price']) * $data[$k]['heyue_cs'] * $data[$k]['cj_num'];
        }
        if(config('web_name') == 'HC'){
            $data[$k]['yingkui'] = round($data[$k]['yingkui']);
        }else{
            $data[$k]['yingkui'] = round($data[$k]['yingkui'],2);
        }
        if($data[$k]['hb_id'] == 1){
            $data[$k]['hb_id'] = '人民币';
        }elseif($data[$k]['hb_id'] == 2){
            $data[$k]['hb_id'] = '港币';
        }elseif($data[$k]['hb_id'] == 3){
            $data[$k]['hb_id'] = '美元';
        }elseif($data[$k]['hb_id'] == 4){
            $data[$k]['hb_id'] = '欧元';
        }
        if($data[$k]['zhanghao_id'] > 0){
            $data[$k]['zhanghao_id'] = '弘苏组';
        }else{
            $data[$k]['zhanghao_id'] = '盈透组';
        }
//        if($data[$k]['tuo_shijian'] > 0){
//            $data[$k]['tuo_shijian'] = $data[$k]['tuo_shijian'];
//        }else{
//            $data[$k]['tuo_shijian'] = $data[$k]['tuo_shijian'];
//        }
//        if($data[$k]['cj_shijian'] > 0){
//            $data[$k]['cj_shijian'] = date('Y-m-d H:i:s',$data[$k]['cj_shijian']);
//        }else{
//            $data[$k]['cj_shijian'] = $data[$k]['tuo_shijian'];
//        }
    }
    if(!empty($data)){
        returnSuccess(['count'=>$count,'data'=>$data]);
    }else{
        returnError('未查询到数据');
    }

}














}
